package hk.hku.hooku.servlet;

import hk.hku.hooku.db.EventDB;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CloseEventServlet extends HttpServlet {
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		//HttpSession session = request.getSession(false);
		String url=request.getParameter("url");
		if(url!=null){
			String close=request.getParameter("close");
			boolean isClose;
			if(close==null||close.equals("1")){
				isClose=true;
			}else{
				isClose=false;
			}
			boolean result;
			Timestamp time = new Timestamp(System.currentTimeMillis());
			if(isClose){
				result=EventDB.closeEvent(url,time);
			}else{
				result=EventDB.openEvent(url);
			}
			PrintWriter writer=response.getWriter();
			if(result){
				writer.write("T");
				writer.write(time.toLocaleString());
			}else{
				writer.write("F");
			}
			
		}
	}
}
